//
//  UIImage+Extension.swift
//  Weibo
//
//  Created by 韩佳 on 16/4/25.
//  Copyright © 2016年 ddg. All rights reserved.
//

import UIKit

extension UIImage {
    class func getScreenShot() -> UIImage{
        let window = UIApplication.sharedApplication().keyWindow
        //开启图形上下文
        UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 1)
        //将图形内容渲染到图形上下文
        window?.drawViewHierarchyInRect(UIScreen.mainScreen().bounds, afterScreenUpdates: false)
        //获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //关闭图形上下文
        UIGraphicsEndImageContext()
        return image
    }
    func scaleToWidth(width: CGFloat)->UIImage{
        if self.size.width < width {
            return self
        }
        let height = width / self.size.width * self.size.height
        let rect = CGRectMake(0, 0, width, height)
        UIGraphicsBeginImageContext(rect.size)
        self.drawInRect(rect)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }
}
